
var newer = newer || {};
newer.gd = newer.gd || {};

newer.LocationMap = function(o) {
	this.root = o.root;
	this.inputId = o.inputId;
	this.mapCanvasId = o.mapCanvasId;
	
	this.map = {};
	this.marker = {};
	this.circle = {};
	this.coption = {};
	this.geocoder = {};
	
	this.lat = 0;
	this.lng = 0;
	this.cradius = 0;
	
	this.init();
};

newer.LocationMap.prototype = {
	init: function() {
		this.initialize();
	},
	
	initialize: function() {
		this.buildMap();
		$("#"+this.inputId).val("");		
		this.buildAddressInput();
		this.buildSetCircleButton();
	},

	buildMap: function() {
		var myOptions = {
		  center: new google.maps.LatLng(37.298181, -121.956266),
		  zoom: 18,
		  mapTypeId: google.maps.MapTypeId.HYBRID
		};

		this.map = new google.maps.Map(document.getElementById(this.mapCanvasId), myOptions);
		this.marker = new google.maps.Marker({map: this.map, draggable: true});
		this.coptions = {
				strokeColor: "#FF0000",
				strokeOpacity: 0.6,
				strokeWeight: 2,
				fillColor: "#FF0000",
				fillOpacity: 0.25,
				map: this.map,
				center: this.marker.getPosition(),
				radius: 20
		};
		this.circle = new google.maps.Circle(this.coptions);
		this.geocoder = new google.maps.Geocoder();

		google.maps.event.addListener(this.map, 'click', function(event) {
			this.placeMarker(event.latLng);
			this.updateCircle();
			this.geocoder.geocode({'latLng': event.latLng}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					if (results[0]) {
						$('#'+this.inputId).val(results[0].formatted_address);
					}
				}
			});		
		});

		google.maps.event.addListener(this.marker, 'drag', function() {
			this.updateCircle();
			this.geocoder.geocode({'latLng': this.marker.getPosition()}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					if (results[0]) {
						$('#'+this.inputId).val(results[0].formatted_address);
						this.lat = results[0].geometry.location.lat();
						this.lng = results[0].geometry.location.lng();
					}
				}
			});
		});

		google.maps.event.addListener(this.circle, 'radius_changed', function() {
			this.cradius = this.circle.radius;
		});

	},
// last time i edit to here
	buildAddressInput: function() {
		var self = this;
		$("#"+this.inputId).autocomplete({
			//This bit uses the geocoder to fetch address values
			source: function(request, response) {
				self.geocoder.geocode( {'address': request.term }, function(results, status) {
					response($.map(results, function(item) {
						return {
						  label:  item.formatted_address,
						  value: item.formatted_address,
						  latitude: item.geometry.location.lat(),
						  longitude: item.geometry.location.lng()
						}
					}));
				})
			},
			//This bit is executed upon selection of an address
			select: function(event, ui) {
				$("#latitude").val(ui.item.latitude);
				$("#longitude").val(ui.item.longitude);
				var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
				self.placeMarker(location);
				self.map.setCenter(location);
			}
		});
	},

	placeMarker: function(location) {
		this.marker.setPosition(location);
		this.lat = location.lat();
		this.lng = location.lng();
	},

	buildSetCircleButton: function() {
//		$("#set-circle-button").on("click", function() {
//			if(!circle.getEditable()) {
//				circle.setEditable(true);
//				$("#set-circle-button").html("Save this radius!");
//			} else {
//				circle.setEditable(false);
//				$("#set-circle-button").html("I want edit my circle!");
//			}
//			updateCircle();
//		});
	},

	updateCircle: function() {
//		circle.setCenter(marker.getPosition());
//		$("#circle-radius-div").html(circle.getRadius() + "m");
	}
}